﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index(string keyword,int? page)
        {
            ViewBag.Title = keyword;
            List<Articles> list = new List<Articles>();
            var query = from c in entities.Articless
                        where c.Title.Contains(keyword) || c.Tag.Contains(keyword)||c.MetaKeyword.Contains(keyword)||c.MetaDescrition.Contains(keyword)||c.ArticleContent.Contains(keyword)
                        select c;
            int pagesize = 20;
            int pageindex = 0;
            if (page == null)
            {
                page = 1;
            }
            pageindex =Convert.ToInt32(page) - 1;
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 19) / 20;

            var search = query.OrderByDescending(o=>o.ID).Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize);
            foreach (Articles a in search)
            {
                a.Categoriess = entities.Categoriess.FirstOrDefault(o => o.ID == a.CategorieID);
                list.Add(a);
            }
            return View(list);
        }

    }
}
